package com.qidian.mes.md.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.md.mapper.MdWorkShopMapper;
import com.qidian.mes.md.service.MdWorkShopService;
import com.qidian.mes.pojo.MdWorkShop;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;

/**
 * @Author 32郗建狄
 * @Company GX631
 * Time 2023/10/30 18:41
 * Description
 */
@Service
public class MdWorkShopServiceImpl implements MdWorkShopService {
    @Autowired
    private MdWorkShopMapper mdWorkShopMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public MESResult get(Long workshopId) {
        if (Objects.nonNull(workshopId)) {
            return MESResult.ok(mdWorkShopMapper.get(workshopId));
        }
        return MESResult.empty();
    }

    @Override
    public MESResult list(int page, int limit, MdWorkShop mdWorkShop) {
        // 开启分页 在当前线程中执行下一条SQL的时候就会进行分页。
        PageHelper.startPage(page,limit);
        List<MdWorkShop> list = mdWorkShopMapper.list(mdWorkShop);
        // 组织一个分页对象。（页码，每页大小，最大页码，数据，总条数）
        PageInfo<MdWorkShop> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult save(MdWorkShop mdWorkShop) {
        if (Objects.nonNull(mdWorkShop)){
            return MESResult.ok(mdWorkShopMapper.save(mdWorkShop));
        }
        return null;
    }

    @Override
    public MESResult delete(Long id) {
        if (Objects.nonNull(id)){
            return MESResult.updateIsOk(mdWorkShopMapper.delete(id));
        }
        return MESResult.empty();
    }

    @Override
    public MESResult deletes(Long[] ids) {
        if (Objects.nonNull(ids)){
            return MESResult.ok(mdWorkShopMapper.deletes(ids)+"");
        }
        return MESResult.empty();
    }

    @Override
    public MESResult update(MdWorkShop mdWorkShop) {
        if (Objects.nonNull(mdWorkShop)){
            return MESResult.updateIsOk(mdWorkShopMapper.update(mdWorkShop));
        }
        return MESResult.empty();
    }

    @Override
    public MESResult generateWorkShopCode() {

        String key = "workshopID";

        StringBuilder id = new StringBuilder();
        // 固定部分-可能需要从数据库查询
        id.append("WS");
        // 时间
        Calendar calendar = Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR) + "" +
                (calendar.get(Calendar.MONTH) + 1) +
                calendar.get(Calendar.DATE));
        // 从redis获取序列
        Long idTemp = stringRedisTemplate.opsForValue().increment(key);
        String idStr = idTemp.toString();
        if(idStr.length() < 5){
            // 前面加0
            for (int i = 0;i < 5 - idStr.length();i++){
                idStr = "0" + idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);
    }
}
